import ykAuthpup from "@/components/yk-authpup/yk-authpup";
import homeApi from "@/static/js/enterpriseServer/index.js"
import demandApi from "@/static/js/enterpriseServer/demand.js"
export default {
	data() {
		return {
			pageShow: false,
			demandId: '',
			demandObj: {},
			joinList: [],
			userId: null,
			isCollect: null,
			//参与需求弹出框
			demandModal: null,
			imgList: [],
			demandDes: '',
			contactTrue: false,
			showChooseImg: false, //是否显示选择图片
			showChooseImgList: [{
				name: '拍摄',
				type: 1,
			}, {
				name: '从相册选择',
				type: 2,
			}], //选择图片方式
			activeImgType: -1, //选择图片方式
			permissionID: '',
		}
	},
	components: {
		ykAuthpup
	},
	onLoad(e) {
		this.demandId = e.id
		this.getDetail()
		this.userId = uni.getStorageSync('userInfo').id
	},
	methods: {
		//获取不同的服务商类型
		getServiceType(type) {
			let text = "";
			switch (type) {
				case 1:
					text = "官方服务商";
					break;
				case 2:
					text = "金牌服务商";
					break;
				case 3:
					text = "银牌服务商";
					break;
				case 4:
					text = "铜牌服务商";
					break;
				case 5:
					text = "普通服务商";
					break;
			}
			return text;
		},

		//获取需求详情
		async getDetail() {
			let res = await demandApi.getDemandDetail({
				demand_id: this.demandId
			})
			if (res.code === 1) {
				this.demandObj = res.data.info
				this.isCollect = res.data.is_collect
				this.joinList = res.data.joinlist
				this.pageShow = true
				this.joinList.forEach((val) => {
					if (val.serviceinfo.user_id == this.userId) {
						this.contactTrue = true;
					}
				});
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//报名参与需求
		signDemand(e) {
			if (this.userId == this.demandObj.user_id) {
				this.$tools.msg('您不能报名参与自己发布的需求')
				return
			}
			if (this.demandObj.status !== 0) {
				return
			}
			this.demandModal = e.currentTarget.dataset.target
		},
		hideModal() {
			this.demandModal = null
		},

		//预览图片信息
		viewImg(val) {
			console.log(val)
			uni.previewImage({
				urls: val.split(',')
			})
		},

		//图片上传
		ChooseImage() {
			let type = this.activeImgType;
			uni.chooseImage({
				count: 5, //默认9
				sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: type == 1 ? ['camera'] : ['album'], //从相册选择
				success: (res) => {
					if (res.tempFilePaths.length > 0) {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.imgUpload(res.tempFilePaths[i]);
							uni.showLoading({
								title: '上传中'
							});
						}
					}
				}
			});
		},
		//获取OSS图片数据
		imgUpload(img) {
			homeApi.uploadImg(img).then(res => {
				let res1 = JSON.parse(res)
				if (res1.code == 1) {
					this.imgList.push(res1.data.fullurl)
					uni.hideLoading();
				} else {
					this.$tools.msg(res1.msg);
				}
			}).catch(err => {
				console.log(err)
			})
		},
		//展示图片
		ViewImage(e) {
			uni.previewImage({
				urls: this.imgList,
				current: e.currentTarget.dataset.url
			});
		},
		//删除图片
		DelImg(e) {
			uni.showModal({
				title: '温馨提示',
				content: '确定要删除此图片？',
				cancelText: '再想想',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						this.imgList.splice(e.currentTarget.dataset.index, 1)
					}
				}
			})
		},

		//报名参与需求
		async saveDemand() {
			if (!uni.getStorageSync('token')) {
				this.$common.handleToken()
				return
			}
			let obj = {
				illustrate: this.demandDes,
				pic: this.imgList.join(','),
				demand_id: this.demandId
			}
			let res = await demandApi.setDemand(obj)
			if (res.code === 1) {
				this.$tools.msg('报名参与成功')
				this.demandModal = null
				this.getDetail()
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//雇佣服务商
		goHire(item) {
			let that = this
			if (!uni.getStorageSync('token')) {
				this.$common.handleToken()
				return
			}
			uni.showModal({
				title: '温馨提示',
				confirmColor: '#0177FD',
				confirmText: '雇佣TA',
				cancelColor: "#999",
				content: '是否确认雇佣该服务商',
				success: function (res) {
					if (res.confirm) {
						let obj = {
							demand_id: that.demandId,
							service_id: item.id
						}
						demandApi.hireService(obj).then(res => {
							if (res.code === 1) {
								that.$tools.msg('雇佣成功')
								that.getDetail()
							} else {
								that.$tools.msg(res.msg)
							}
						})
					}
				}
			})
		},

		//收藏需求
		async setCollect() {
			if (!uni.getStorageSync('token')) {
				this.$common.handleToken()
				return
			}
			let obj = {
				id: this.demandId,
				type: 14
			}
			let res = await homeApi.setCollect(obj)
			if (res.code === 1) {
				this.isCollect = 1 - this.isCollect
				if (this.isCollect === 1) {
					this.$tools.msg('收藏成功')
				} else {
					this.$tools.msg('取消收藏')
				}
			}
		},

		//与该商家沟通
		goContact(item) {
			if (!uni.getStorageSync('token')) {
				this.$common.handleToken()
				return
			}
			uni.setStorageSync("to_zim_userid", item.serviceinfo.user_id);
			uni.navigateTo({
				url: '/enterpriseServer/chatRoom/index?id=' + item.serviceinfo.user_id
			})
		},

		//服务商联系客户
		contactUser() {
			uni.setStorageSync("to_zim_userid", this.demandObj.user_id);
			uni.navigateTo({
				url: '/enterpriseServer/chatRoom/index?id=' + this.demandObj.user_id
			})
		},

		chooseImgType() {
			this.showChooseImg = true
		},

		//用户授权权限后的回调
		changeAuth() {
			//这里是权限通过后执行自己的代码逻辑
			this.ChooseImage()
		},

		selectClick(e) {
			this.activeImgType = e.type
			if (e.type == 1) {
				this.openAuth('CAMERA')
			} else if (e.type == 2) {
				this.openAuth('WRITE_EXTERNAL_STORAGE')
			}
		},

		openAuth(permissionID) {
			let sysTemInfo = uni.getSystemInfoSync().brand; //获取手机品牌
			if (sysTemInfo == 'huawei') {
				this.permissionID =
					permissionID; //这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
				setTimeout(() => {
					this.$refs['authpup'].open();
				}, 500)
			} else {
				this.ChooseImage()
			}
		},
	}
}